package com.ming.projecttest;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.ming.projecttest.base.BasePermissionActivity;
import com.ming.projecttest.listener.OnRequestPermissionListener;
import com.ming.projecttest.ui.FragmentTestActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 工程测试用例
 */
public class MainActivity extends BasePermissionActivity implements AdapterView.OnItemClickListener {

    public static final String TAG = "Sample";

    private String[] mStr = {"请求权限", "RxPermissions", "动态权限封装","Fragment"};
    private ArrayAdapter<String> mArrayAdapter;
    private ListView mListView;

    /**
     * 1. 动态权限
     * 确定权限 --> call phone
     * 判断权限
     * 申请权限
     * 处理权限
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.mListView);
        mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStr);
        mListView.setAdapter(mArrayAdapter);

        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                requestPermissions();
                break;
            case 1:
                rxPermiss();
                break;
            case 2:
                checkPermissions(new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA}, 100, new OnRequestPermissionListener() {
                    @Override
                    public void onSuccessful(int[] grantResults) {
                        for (int i=0; i<grantResults.length; i++) {
                            Log.i(TAG, grantResults[i] + "");
                        }
                    }

                    @Override
                    public void onFailure() {

                    }

                    @Override
                    public void onNullPermission() {

                    }
                });
                break;
            case 3:
                startActivity(new Intent(this, FragmentTestActivity.class));
                break;
        }
    }

    // 打电话
    private void callPhone(String phone) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("tel" + phone));
        startActivity(intent);
    }

    // RxPermissons 框架请求权限
    private void rxPermiss() {
        RxPermissions rxPermissions = new RxPermissions(this);
        // 请求权限
        rxPermissions
                .request(Manifest.permission.CALL_PHONE,
                        Manifest.permission.CAMERA)
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        Log.i(TAG, "结果:" + aBoolean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    // 普通的请求权限
    private void requestPermissions() {
        // 判断权限
        int permissionCheck = ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            // 申请权限
            Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
            // 判断用户是否拒绝过该权限
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
                // true 说明已经拒绝过 | 需要解释权限
                Toast.makeText(this, "hello 我需要这个权限", Toast.LENGTH_SHORT).show();
                // 申请权限
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 100);
            } else {
                // 申请权限
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 100);
            }
        } else {
            callPhone("1149");
        }
    }

//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        Log.i("Test", "requestCode" + requestCode + "permission:" + permissions.length);
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//
//        if (requestCode == 100) {
//            if (grantResults.length > 0) {
//                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    callPhone("119");
//                } else {
////                    Log.i("Test", "requestCode" + requestCode + "permission:" + permissions.length);
//                    Toast.makeText(this, "这样将无法打电话", Toast.LENGTH_SHORT).show();
//                }
//            }
//        }
//    }
}
